<?php 
header("Content-type:text/html;charset=utf-8");
require_once("model.php");
class videomodel extends model{
	function __construct(){
		parent::__construct();
	}
	function getvideolist(){
		$sql = "select * from videos join videotype on videos.tid = videotype.tid ";
		$data = $this->connection->query($sql);
		return $data; 
	}
	function addvideo($data){
		//var_dump($data);
		if(!empty($_FILES["pic"]["name"])){
		  	$pic = $this->uploadfile('pic','images/',array('jpg','gif','png'));
		}
		else{
		  	$pic='';
		}
		//echo $_FILES["pic"]["name"];
		$sql = "insert into videos(vid,videoname,tid,pic,intro,uploaddate,uploadadmin,hittimes,downtimes,address) values(null,'".$data['videoname']."','".$data['tid']."','".$pic."','".$data['intro']."','".$data['uploaddate']."','".$data['uploadadmin']."','".$data['hittimes']."','".$data['downtimes']."','".$data['address']."')";
		//echo $sql;
		$result = $this->connection->query($sql);
		return $result;
	}
	function getvideobyid($id){
		$sql = "select * from videos left join videotype on videos.tid= videotype.tid where vid=".$id."";
		//echo $sql;
		$data = $this->connection->query($sql);
		return $data;
	}
	function GetVideoBySearch($data){
		$data = $data['search'];
		$sql = "select * from videos left join videotype on videos.tid= videotype.tid where videoname like '%".$data."%'";
		//echo $sql;
		$data = $this->connection->query($sql);
		//var_dump($data);
		return $data;
	}
	function updatevideo($data){
		if(!empty($_FILES["pic"]["name"])){
		  	$pic = $this->uploadfile('pic','images/',array('jpg','gif','png'));
		}
		else{
		  	$pic='';
		}
		//echo $pic;
		$videos = array(
			'vid'=>$data['vid'],
			'videoname'=>$data['videoname'],
			'tid'=>$data['tid'],
			'pic'=>$pic,
			'intro'=>$data['intro'],
			'uploaddate'=>$data['uploaddate'],
			'uploadadmin'=>$data['uploadadmin'],
			'hittimes'=>$data['hittimes'],
			'downtimes'=>$data['downtimes'],
			'address'=>$data['address']
			);
		//var_dump($videos);
		$sets = '';
		foreach($videos as $key =>$val){
			$sets .=  $key."='".$val."',";
		}
		$sets = rtrim($sets,',');
		//echo $sets;
	    $sql='UPDATE videos SET '.$sets.' where vid='.$data['vid'].' ';  
	    //echo $sql;
	    
	    $res=mysql_query($sql);

	    if ($res){
	        return mysql_affected_rows();
	    }else {
	        return false;
	    }
	}
	 function delvideo($id){
    	//echo $id;
    	$sql = "delete from videos where vid=".$id." ";
    	//echo $sql;
    	$data = $this->connection->query($sql);
    	return $data;
    }

}


 ?>